// Script executed in dash.html

$(function () {

    addCourselist();

    addCourseContent();

});


function addCourselist() {
    // Todo: Get list of recent courses from server
    if(courses) {    
        courses.forEach(function (item) {
            var css = { "background-image": "url('" + item.icon + "')" };
            $("<button>").text(item.name).css(css).appendTo($(".citems"));
        });
    }
}

function addCourseContent() {
    
    // Todo: Get course data from server
    
    if(courseData) {
    
        var $subhead = $(".dashtab .dtsubhead"); 
        var $rowtemp = $(".dashtab .dtdatarow").remove().removeClass("hidden");
        var $tab = $subhead.find(".dttitle").text(courseData.title).end().parent();
                
        var setProgress = function($div, percentage) { 
            $div.find("span span").css("width", percentage + "%");
        }
        
        var fix = function(num) { 
            return ((num=Math.floor(num)%100)<10?"0":"")+num; 
        }        
        
        var objTotal=0, objDone=0, expGained=0;
        courseData.chapters.forEach(function(chapter, i) {
            var $row = $rowtemp.clone();                                
            var tState = function() { $(this).toggleClass("max"); }
            
            var txt = "CHAPTER " + (i+1) + ": " +chapter.title;
            
            
            $rowt = $row.find(".dttitle").click(tState).html($("<div>").text(txt));
                        
            var exp = 0, done = 0, total = chapter.subs.length;
            chapter.subs.forEach(function(sub) {
                var div = "<div " + (sub.exp?" class='exp'":"") + ">";                
                $(div).text("OBJECTIVE: " + sub.obj).appendTo($rowt);
                done+=sub.done?1:0;
                exp+=sub.exp?1:0;
            });
            
            setProgress($row.find(".dtprogress"), done / total * 100);
            setProgress($row.find(".dtscore"), exp / total * 100);
            
            objDone += done; expGained +=  exp; objTotal += total;            
            
            $row.find(".dtgobtn").attr("href","learn.html");
            
            $row.appendTo($tab);
        });
        
        setProgress($subhead.find(".dtprogress"), objDone / objTotal * 100);
        $subhead.find(".dtscore > span").first().text(fix(expGained))
                .siblings().last().text(fix(objTotal));
    }
}


/*====================================================*/
/*  Dummy Data to be replaced by data from the server */
/*====================================================*/

var courses = [{ name: "Microsoft Excel", icon: "img/courses/xls.jpg" },
                   { name: "Microsoft Word", icon: "img/courses/doc.jpg" },
                   { name: "Microsoft Powerpoint", icon: "img/courses/ppt.jpg" },
                   { name: "Adobe Photoshop", icon: "img/courses/ps.jpg" },
                   { name: "Google Docs", icon: "img/courses/gd.jpg" }];

var courseData = {
    title: "Microsoft Excel Basics",
    chapters: [
        {
            title: "Conditional formatting",
            subs: [{obj: "A", done:true, exp:true}, 
                   {obj: "B", done:true, exp:true}, 
                   {obj: "C", done:true, exp:false}]
        },
        {
            title: "Charts And Graphs",
            subs: [{obj: "Bar Charts", done:true, exp:true}, 
                   {obj: "Pie Charts", done:false, exp:false}, 
                   {obj: "Pivot Tables", done:false, exp:false}]
        },
        {
            title: "Conditional formatting",
            subs: [{obj: "A", done:true, exp:false}, 
                   {obj: "B", done:false, exp:false}, 
                   {obj: "C", done:false, exp:false}]
        }
    ]
};
